/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.graficaluna.dao.otros;

import dto.ClienteDTO;

import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

/**
 *
 * @author ralf
 */
public class ClienteDAO {
    
    
    private SqlSessionFactory sqlSessionFactory;

    public ClienteDAO() {
        sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
    }
    
    
    public List<ClienteDTO> listarClientes() {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            List<ClienteDTO> list = session.selectList("clienteSQL.listarClientes");
            return list;
        }catch(Exception e){                       
            return null;
        } finally {
            session.close();
        }
    }
     
    public void insertarCliente(ClienteDTO p){
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.insert("clienteSQL.insertarCliente", p);        
            session.commit();
        } finally {
            session.close();
        }
    }
    
    public void eliminarCliente(Long codigo) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.delete("clienteSQL.eliminarCliente", codigo);
            session.commit();
        } finally {
            session.close();
        }
    }
    
    public void actualizarCliente(ClienteDTO p) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            session.update("clienteSQL.actualizarCliente", p);
            session.commit();
        } finally {
            session.close();
        }
    }
    
    public ClienteDTO obtenerClientePorId(Long codigo) {   
        SqlSession session = sqlSessionFactory.openSession();
        try {            
            ClienteDTO contact = (ClienteDTO) session.selectOne("clienteSQL.obtenerClientePorId", codigo);
            return contact;
        } finally {
            session.close();
        }
    }
    
    
    public List<ClienteDTO> buscarClientesPorFiltro(String texto) {
        SqlSession session = sqlSessionFactory.openSession();
        try {
            List<ClienteDTO> list = session.selectList("clienteSQL.buscarClientesPorFiltro", "%"+texto+"%");
            return list;
        }catch(Exception e){                       
            return null;
        } finally {
            session.close();
        }
    }
    
}
